package com.example.zhunongzonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhunongzonghe.beans.ZnScenicEvaluateBean;
import com.example.zhunongzonghe.dao.ZnScenicEvaluateMapper;
import com.example.zhunongzonghe.entity.ZnScenicEvaluate;
import com.example.zhunongzonghe.entity.ZnScenicspot;
import com.example.zhunongzonghe.entity.ZnUsers;
import com.example.zhunongzonghe.service.IZnUsersService;
import com.example.zhunongzonghe.service.ZnScenicEvaluateLikeService;
import com.example.zhunongzonghe.service.ZnScenicEvaluateService;
import com.example.zhunongzonghe.utils.BeanCopyUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author 蔡某人
* @description 针对表【zn_scenic_evaluate】的数据库操作Service实现
* @createDate 2024-04-25 17:05:46
*/
@Service
public class ZnScenicEvaluateServiceImpl extends ServiceImpl<ZnScenicEvaluateMapper, ZnScenicEvaluate>
    implements ZnScenicEvaluateService{
    @Resource
    private ZnScenicEvaluateMapper scenicEvaluateMapper;
    @Resource
    private IZnUsersService usersService;
    @Resource
    private ZnScenicEvaluateLikeService evaluateLikeService;

    @Override
    public Page<ZnScenicEvaluateBean> selectNoLoginEvaluateAll(Integer currentPage,
                                                               Integer pageSize, Integer ScenicId) {
        Page<ZnScenicEvaluate> page = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<ZnScenicEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnScenicEvaluate::getScenicId,ScenicId);
        Page<ZnScenicEvaluate> scenicEvaluatePage = scenicEvaluateMapper.selectPage(page,queryWrapper);
        Page<ZnScenicEvaluateBean> beanPage = BeanCopyUtils.copyBeanPage(scenicEvaluatePage,ZnScenicEvaluateBean.class);
        double averageMarks = averageScore(ScenicId);
        Long CommentNumBer = scenicEvaluateMapper.selectCount(queryWrapper);
        beanPage.getRecords().forEach(EvaluateBean->{
            ZnUsers znUsers = usersService.getUserName(EvaluateBean.getUsername());
            EvaluateBean.setAverageMarks(averageMarks);
            EvaluateBean.setAvatar(znUsers.getAvatar());
            EvaluateBean.setNickName(znUsers.getNickname());
            EvaluateBean.setCommentNumBer(CommentNumBer);
            EvaluateBean.setLikeCount(evaluateLikeService.likeCounts(EvaluateBean.getId()));
        });
        return beanPage;
    }

    @Override
    public Page<ZnScenicEvaluateBean> selectEvaluateAll(Integer currentPage,Integer pageSize
            ,Integer ScenicId, String username) {
        Page<ZnScenicEvaluate> page = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<ZnScenicEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnScenicEvaluate::getScenicId,ScenicId);
        Page<ZnScenicEvaluate> scenicEvaluatePage = scenicEvaluateMapper.selectPage(page,queryWrapper);
        ZnUsers znUsers = usersService.getUserName(username);
        Page<ZnScenicEvaluateBean> beanPage = BeanCopyUtils.copyBeanPage(scenicEvaluatePage,ZnScenicEvaluateBean.class);
        Long CommentNumBer = scenicEvaluateMapper.selectCount(queryWrapper);
        double averageMarks = averageScore(ScenicId);
        beanPage.getRecords().forEach(EvaluateBean->{
            ZnUsers znUsersEva = usersService.getUserName(EvaluateBean.getUsername());
            EvaluateBean.setAvatar(znUsersEva.getAvatar());
            EvaluateBean.setNickName(znUsersEva.getNickname());
            EvaluateBean.setAverageMarks(averageMarks);
            EvaluateBean.setCommentNumBer(CommentNumBer);
            EvaluateBean.setLinked(evaluateLikeService.isLiked(EvaluateBean.getId(),znUsers.getId()));
            EvaluateBean.setLikeCount(evaluateLikeService.likeCounts(EvaluateBean.getId()));
        });
        return beanPage;
    }

    @Override
    public void addEvaluate(ZnScenicEvaluateBean znScenicEvaluateBean, String userName) {
        ZnScenicEvaluate znScenicEvaluate = BeanCopyUtils.copyBean(znScenicEvaluateBean,ZnScenicEvaluate.class);
        znScenicEvaluate.setUsername(userName);
        znScenicEvaluate.setCreateTime(LocalDateTime.now());
        znScenicEvaluate.setUpdateTime(LocalDateTime.now());
        scenicEvaluateMapper.insert(znScenicEvaluate);
    }

    @Override
    public void editEvaluate(ZnScenicEvaluateBean znScenicEvaluateBean) {
        ZnScenicEvaluate znScenicEvaluate = BeanCopyUtils.copyBean(znScenicEvaluateBean,ZnScenicEvaluate.class);
        znScenicEvaluate.setUpdateTime(LocalDateTime.now());
        scenicEvaluateMapper.updateById(znScenicEvaluate);
    }

    @Override
    public Page<ZnScenicEvaluateBean> getMagEvaluateAll(Integer currentPage, Integer pageSize, Object searchInfo) {
        Page<ZnScenicEvaluateBean> page = new Page<>(currentPage,pageSize);
        MPJLambdaWrapper<ZnScenicEvaluate> mpjLambdaWrapper = new MPJLambdaWrapper<ZnScenicEvaluate>()
                .selectAll(ZnScenicEvaluate.class)
                .select(ZnUsers::getNickname)
                .select(ZnScenicspot::getSpotName)
                .like(ZnScenicEvaluate::getContent,searchInfo)
                .or().like(ZnUsers::getNickname,searchInfo)
                .or().like(ZnScenicspot::getSpotName,searchInfo)
                .innerJoin(ZnUsers.class,ZnUsers::getUserName,ZnScenicEvaluate::getUsername)
                .innerJoin(ZnScenicspot.class,ZnScenicspot::getSpotId,ZnScenicEvaluate::getScenicId);
        Page<ZnScenicEvaluateBean> beanPage = scenicEvaluateMapper.selectJoinPage(page,ZnScenicEvaluateBean.class,mpjLambdaWrapper);
        return beanPage;
    }

    private double averageScore(Integer ScenicId){
        LambdaQueryWrapper<ZnScenicEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnScenicEvaluate::getScenicId,ScenicId);
        List<ZnScenicEvaluate> evaluateList = scenicEvaluateMapper.selectList(queryWrapper);
        double averageScore = evaluateList.stream()
                .mapToDouble(ZnScenicEvaluate::getScore)
                .average()
                .orElse(0.0);
        return Math.round(averageScore * 10.0) / 10.0;
    }
}




